package com.loong.tool.secret.base;

import com.loong.tool.secret.exception.InvalidCodeException;

/**
 * base64处理类
 */
public class Base64 {

	private static final byte INT[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
			-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1,
			63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
			11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31,
			32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 };

	private static final char BASE64[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
			'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4',
			'5', '6', '7', '8', '9', '+', '/' };

	/**
	 * Base64转码
	 * 
	 * @param bytes 待处理字节数组
	 * @return Base64字符串
	 */
	public static String encode(byte[] bytes) {

		int l = bytes.length;
		int nGroup = l / 3;
		int pGroup = l - 3 * nGroup;
		int resutL = 4 * ((l + 2) / 3);
		StringBuilder result = new StringBuilder(resutL);
		int inCursor = 0;
		for (int i = 0; i < nGroup; i++) {
			int byte0 = bytes[inCursor++] & 0xff;
			int byte1 = bytes[inCursor++] & 0xff;
			int byte2 = bytes[inCursor++] & 0xff;
			result.append(BASE64[byte0 >> 2]);
			result.append(BASE64[(byte0 << 4) & 0x3f | (byte1 >> 4)]);
			result.append(BASE64[(byte1 << 2) & 0x3f | (byte2 >> 6)]);
			result.append(BASE64[byte2 & 0x3f]);
		}
		if (pGroup != 0) {
			int byte0 = bytes[inCursor++] & 0xff;
			result.append(BASE64[byte0 >> 2]);
			if (pGroup == 1) {
				result.append(BASE64[(byte0 << 4) & 0x3f]);
				result.append("==");
			} else {
				int byte1 = bytes[inCursor++] & 0xff;
				result.append(BASE64[(byte0 << 4) & 0x3f | (byte1 >> 4)]);
				result.append(BASE64[(byte1 << 2) & 0x3f]);
				result.append('=');
			}
		}
		return result.toString();
	}

	/**
	 * Base64解码
	 * 
	 * @param str Base64字符串
	 * @return 字节
	 * @throws InvalidCodeException 解码错误
	 */
	public static byte[] decode(String str) throws InvalidCodeException {

		int l = str.length();
		int group = l / 4;
		if (4 * group != l)
			throw new InvalidCodeException("error code");
		int lGroup = 0;
		int nGroup = group;
		if (l != 0) {
			if (str.charAt(l - 1) == '=') {
				lGroup++;
				nGroup--;
			}
			if (str.charAt(l - 2) == '=')
				lGroup++;
		}
		byte[] result = new byte[3 * group - lGroup];
		int inCursor = 0, outCursor = 0;
		for (int i = 0; i < nGroup; i++) {
			int ch0 = toInt(str.charAt(inCursor++), INT);
			int ch1 = toInt(str.charAt(inCursor++), INT);
			int ch2 = toInt(str.charAt(inCursor++), INT);
			int ch3 = toInt(str.charAt(inCursor++), INT);
			result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
			result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
			result[outCursor++] = (byte) ((ch2 << 6) | ch3);
		}
		if (lGroup != 0) {
			int ch0 = toInt(str.charAt(inCursor++), INT);
			int ch1 = toInt(str.charAt(inCursor++), INT);
			result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
			if (lGroup == 1) {
				int ch2 = toInt(str.charAt(inCursor++), INT);
				result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
			}
		}
		return result;
	}

	private static int toInt(char c, byte[] alphaToInt) throws InvalidCodeException {

		int result = alphaToInt[c];
		if (result < 0)
			throw new InvalidCodeException("code error: " + c);
		return result;
	}
}
